<?php

class BlogService extends Service_Base implements ISingleton 
{
	
	/**
	 *
	 * @var BlogService
	 */
	private static $_instance;
	
	/**
	 *
	 * @return BlogService
	 */
	public static function getInstance()
	{
		if (!self::$_instance) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	/**
	 *
	 * @param int $id
	 * @return Db_Row|null
	 */
	public function getBlogById($id)
	{
		if (!$id) {
			return null;
		}
		return $this->getAdapter()->getBlogPeer()->findById($id)->current();
	}
	
	public function getPosts(BlogRow $blogRow, $limit, $page) 
	{
		if (!$blogRow) {
			return null;
		}
		$select = $this->getAdapter()->getPostPeer()->select()->limitPage($page, $limit);
		return $blogRow->getPostRowsetByBlogId($select);
	}
	
	public function getLastBlogs($limit)
	{
		$peer = BlogPeer::getInstance();
		$select = $peer->select()->order('created desc')->limit($limit);
		return $peer->fetchAll($select);
	}
	
	public function getBlogsByMostSubscribers($limit)
	{
		$peer = BlogPeer::getInstance();
		$select = $peer->select()->order('subscriber_count desc')->limit($limit);
		return $peer->fetchAll($select);
	}
}